package jp.ne.ayamesoft.ayamevoicedialer;

import java.util.ArrayList;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CMemberListAdapter extends BaseAdapter {
	private LayoutInflater layoutInflater = null;
	private ArrayList<CMemberData> listMemberData = null;
	private Context mcontext;
	final int MENU_SELECT_CONTACTS = 1;
	final int MENU_ABOUT = 2;
	final int MENU_B_SELECT_CONTACT = 0;
	final int MENU_B_DELETE = 1;
	private Handler mhandler;
	private ViewHolder holder;

	public CMemberListAdapter(Context context,
			ArrayList<CMemberData> _listMemberData, Handler mhandler1) {
		this.mcontext = context;
		// layoutInflater = LayoutInflater.from(context);
		layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		listMemberData = _listMemberData;
		this.mhandler = mhandler1;
	}

	@Override
	public int getCount() {
		return listMemberData.size();
	}

	@Override
	public Object getItem(int arg0) {
			return listMemberData.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int position1, View convertView, ViewGroup arg2) {

		if (convertView == null) {
			// AyameLOG.D("getView:null position=[" + String.valueOf(position) +
			// "]");
			// convertView = layoutInflater.inflate(R.layout.member_list, null);
			convertView = layoutInflater.inflate(R.layout.member_list, arg2,
					false);
			LinearLayout a = (LinearLayout)convertView.findViewById(R.id.idMemberLayout);
			a.setOnClickListener(new OnClickListener(
					) {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Bundle bundle = new Bundle();
					bundle.putString("number", listMemberData.get(position1).getStringNumber());
					bundle.putInt("a", position1);
					mhandler.sendMessage(mhandler.obtainMessage(0, bundle));
				}
			});
			holder = new ViewHolder();
			System.out.println("value of position  = " + position1);
			// holder.id = (TextView) convertView.findViewById(R.id.idId);

			holder.nickname = (TextView) convertView
					.findViewById(R.id.idNickname);
			holder.name = (TextView) convertView.findViewById(R.id.idName);
			holder.number = (TextView) convertView.findViewById(R.id.idNumber);
			//holder.image1 = (ImageButton) convertView.findViewById(R.id.image1);
			holder.image2 = (ImageButton) convertView.findViewById(R.id.image2);
			convertView.setTag(holder);
		} else {
			// AyameLOG.D("getView:not null position=[" +
			// String.valueOf(position) + "] view=[" + convertView + "]");
			holder = (ViewHolder) convertView.getTag();
		}

		// holder.id.setText(String.valueOf(listMemberData.get(position).getIntId()));

		holder.nickname.setText(listMemberData.get(position1)
				.getStringNickname());
		holder.name.setText("Name : "+listMemberData.get(position1).getStringName());
		holder.number.setText("Number : "+listMemberData.get(position1).getStringNumber());
//		holder.image1.setBackgroundResource(R.drawable.divider_vertical_dark);
		holder.image2.setBackgroundResource(android.R.drawable.sym_action_call);
//		holder.image1.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//
//				bundle.putInt("a", position1);
//				mhandler.sendMessage(mhandler.obtainMessage(0, bundle));
//
//			}
//		});
		holder.image2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				Bundle bundle = new Bundle();
				bundle.putString("number", listMemberData.get(position1).getStringNumber());
				bundle.putInt("call", position1);
				
				mhandler.sendMessage(mhandler.obtainMessage(0, bundle));

			}
		});
//		holder.nickname.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//
//				Bundle bundle = new Bundle();
//				bundle.putInt("a", position1);
//				mhandler.sendMessage(mhandler.obtainMessage(0, bundle));
//			}
//		});
//		holder.name.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//
//				Bundle bundle = new Bundle();
//				bundle.putInt("a", position1);
//				mhandler.sendMessage(mhandler.obtainMessage(0, bundle));
//
//			}
//		});
//		holder.number.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//
//				Bundle bundle = new Bundle();
//				bundle.putInt("a", position1);
//				mhandler.sendMessage(mhandler.obtainMessage(0, bundle));
//			}
//		});
		return convertView;
	}

	static class ViewHolder {

		TextView nickname;
		TextView name;
		TextView number;
		ImageButton image1;
		ImageButton image2;

	}
}
